package org.gp.demo.file.xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXDemo1 {
	static ArrayList array=new ArrayList();
	public static void main(String[] args) {
		try {
			InputStream is = new FileInputStream(new File(
					"F:/shenpi/config/gUnit.xml"));
			parserXML(is);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void parserXML(InputStream in) {

		DefaultHandler handler = new DefaultHandler() {

			@Override
			public void startElement(String namespaceURI, String lname,
					String qname, Attributes attrs) throws SAXException {
				// TODO Auto-generated method stub
				if (lname.toLowerCase().equals("privilege") && attrs != null) {
					if(array.contains(attrs.getValue("id"))){
					System.out.println("privilege_id:"
							+ attrs.getValue("privilege_id") + "  id:"
							+ attrs.getValue("id"));
					}else{
						array.add(attrs.getValue("id"));
					}
				}
			}

		};

		SAXParserFactory sf = SAXParserFactory.newInstance();
		sf.setNamespaceAware(true);
		try {
			SAXParser saxParser = sf.newSAXParser();
			saxParser.parse(in, handler);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
